package com.cy.pj.common.web;


import org.apache.shiro.ShiroException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import com.cy.pj.common.pojo.JsonResult;

/**
 * @ControllerAdvice  此注解描述的类，spring mvc会认为它是一个控制层全局异常处理对象
 * @author Administrator
 *
 */
//@ControllerAdvice
//@ResponseBody
@RestControllerAdvice	//@RestControllerAdvice===@ControllerAdvice+@ResponseBody
public class GlobalExceptionHandler {
	
	
	 	@ExceptionHandler(ShiroException.class) 
		public JsonResult doHandleShiroException(
				ShiroException e) {
			JsonResult r=new JsonResult();
			r.setState(0);
			if(e instanceof UnknownAccountException) {
				r.setMessage("账户不存在");
			}else if(e instanceof LockedAccountException) {
				r.setMessage("账户已被禁用");
			}else if(e instanceof IncorrectCredentialsException) {
				r.setMessage("密码不正确");
			}else if(e instanceof AuthorizationException) {
				r.setMessage("没有此操作权限");
			}else {
				r.setMessage("系统维护中");
			}
			e.printStackTrace();
			return r;
		}
	
	
	/**
	 * @ExceptionHandler  在此注解后面加类，则代表只会处理这个类的异常，如果不加，则会处理所有异常
	 * @param e                                                                                                                                                        
	 * @return
	 */
	@ExceptionHandler(RuntimeException.class)
	public JsonResult doHandleRuntimeException(RuntimeException e){
		//方法内部实现异常处理
		//例如：输出异常，封装异常
		//输出或打印异常
		e.printStackTrace();
	 //封装异常信息
	 return new JsonResult(e);
	 }
	
	
	

}
